//
// Created by HP on 2024/2/23.
//

#include "aurora_wave.h"
#include "rtthread.h"

uint16_t __attribute__((section(".ccmram"))) wave_data[3][WAVE_X_LEN]; // 波形数据
Aurora_Wave::Aurora_Wave(LCD_Driver *lcd,Aurora_Font *font){
    _lcd = lcd;
    _font = font;
    Yaxis_Max = 1;
    Yaxis_Min = 0;//-ARR_10Khz;

    YStart = 100;
    Yend   = 170;


    data_in = 0;

    for(int i=0;i<WAVE_X_LEN;i++){
        wave_data[0][i] = 35;
        wave_data[1][i] = 35;
        wave_data[2][i] = 35;
    }
    wave_color[0] = 0x0fb0;
    wave_color[1] = 0x00f8;
    wave_color[2] = 0xf000;

    ui2foc_msg.type = enable_wave;
    ui2foc_msg.wave_type = LFOC_TaTbTc;
    ui2foc_msg.cmd = IS_CMD;
}


//取消波形显示
Aurora_Wave::~Aurora_Wave(){
    ui2foc_msg.type = enable_wave;
    ui2foc_msg.wave_type = NONE;
    ui2foc_msg.cmd = IS_CMD;
    rt_thread_mdelay(20);
}


void Aurora_Wave::add_Driver(LCD_Driver *lcd,Aurora_Font *font){
    _lcd = lcd;
    _font = font;
}

void Aurora_Wave::wave_label_init(){
    wave_index = 1;

    title_label.set_font_driver(_font);
    title_label.set_lcd_driver(_lcd);
    title_label.set_font_size(Aurora_Font::font_size_big);
    title_label.set_text("波形显示");
    title_label.set_pos(100,0);
    title_label.show();

    motor_label.set_font_driver(_font);
    motor_label.set_lcd_driver(_lcd);
    motor_label.set_font_size(Aurora_Font::font_size_middle);
    motor_label.set_text("左电机");
    motor_label.set_pos(10,10);
    motor_label.show();



    wave1_label.set_font_driver(_font);
    wave1_label.set_lcd_driver(_lcd);
    wave1_label.set_font_size(Aurora_Font::font_size_middle);
    wave1_label.set_text("Ta");
    wave1_label.set_pos(10,40);
    wave1_label.set_color(wave_color[0]);
    wave1_label.show();

    wave2_label.set_font_driver(_font);
    wave2_label.set_lcd_driver(_lcd);
    wave2_label.set_font_size(Aurora_Font::font_size_middle);
    wave2_label.set_text("Tb");
    wave2_label.set_pos(145,40);
    wave2_label.set_color(wave_color[1]);
    wave2_label.show();

    wave3_label.set_font_driver(_font);
    wave3_label.set_lcd_driver(_lcd);
    wave3_label.set_font_size(Aurora_Font::font_size_middle);
    wave3_label.set_text("Tc");
    wave3_label.set_pos(255,40);
    wave3_label.set_color(wave_color[2]);
    wave3_label.show();

}

/** 更新显示的最大值最小值 **/
void Aurora_Wave::update_maxmin_value(float data_max,float data_min){
    char data[20];
}

/** 设置Y轴 显示的 最大值 最小值**/
void Aurora_Wave::setYMaxMin(float max,float min){
    Yaxis_Max = max;
    Yaxis_Min = min;
}
/** 添加波形数据 **/
void Aurora_Wave::addWaveData(Aurora_FOC2UI_Wave data){

    uint16_t i;
    float Yaxis_Max_sub_Yaxis_Min = (data.wave_value_max - data.wave_value_min);
    float Yend_sub_YStart = Yend - YStart;

    for( i = 0; i < WAVE_X_LEN - 1;i++){
        wave_data[0][i] = wave_data[0][i+1];
        wave_data[1][i] = wave_data[1][i+1];
        wave_data[2][i] = wave_data[2][i+1];
    }
    data.wave1_value = (data.wave1_value > data.wave_value_max ) ? data.wave_value_max  : data.wave1_value;
    data.wave1_value = (data.wave1_value < data.wave_value_min) ? data.wave_value_min : data.wave1_value;
    wave_data[0][WAVE_X_LEN -1] = (uint16_t) Yend_sub_YStart -
                        (uint16_t) (((data.wave1_value - data.wave_value_min) / (Yaxis_Max_sub_Yaxis_Min)) * Yend_sub_YStart);
    wave_data[0][WAVE_X_LEN -1] = (wave_data[0][WAVE_X_LEN-1] == Yend_sub_YStart) ? Yend_sub_YStart - 1 : wave_data[0][WAVE_X_LEN-1];

    data.wave2_value = (data.wave2_value > data.wave_value_max) ? data.wave_value_max : data.wave2_value;
    data.wave2_value = (data.wave2_value < data.wave_value_min) ? data.wave_value_min : data.wave2_value;
    wave_data[1][WAVE_X_LEN-1] = (uint16_t) Yend_sub_YStart -
                        (uint16_t) (((data.wave2_value - data.wave_value_min) / (Yaxis_Max_sub_Yaxis_Min)) * Yend_sub_YStart);
    wave_data[1][WAVE_X_LEN-1] = (wave_data[1][WAVE_X_LEN-1] == Yend_sub_YStart) ? Yend_sub_YStart - 1 : wave_data[1][WAVE_X_LEN-1];

    data.wave3_value = (data.wave3_value > data.wave_value_max) ? data.wave_value_max : data.wave3_value;
    data.wave3_value = (data.wave3_value < data.wave_value_min) ? data.wave_value_min : data.wave3_value;
    wave_data[2][WAVE_X_LEN-1] = (uint16_t) Yend_sub_YStart -
                        (uint16_t) (((data.wave3_value - data.wave_value_min) / (Yaxis_Max_sub_Yaxis_Min)) * Yend_sub_YStart);
    wave_data[2][WAVE_X_LEN-1] = (wave_data[2][WAVE_X_LEN-1] == Yend_sub_YStart) ? Yend_sub_YStart - 1 : wave_data[2][WAVE_X_LEN-1];



    data_in += 1;
    if(data_in == 240)
        data_in = 0;
    else
        return;

    memset(lcd_buff,0xff,WAVE_X_LEN * (Yend_sub_YStart)*2);

    uint16_t index = ((Yend_sub_YStart)/2)*WAVE_X_LEN;//(uint16_t)((Yend_sub_YStart)/2)*WAVE_X_LEN
    for(i=0;i<WAVE_X_LEN-1;i++){
        bresenham(i,wave_data[0][i],i+1,wave_data[0][i+1],wave_color[0]);
        bresenham(i,wave_data[1][i],i+1,wave_data[1][i+1],wave_color[1]);
        bresenham(i,wave_data[2][i],i+1,wave_data[2][i+1],wave_color[2]);
        lcd_buff[index+i] = 0x0000;     //绘制X轴
    }

    //绘制Y轴
    bresenham(WAVE_X_LEN/2,0,WAVE_X_LEN/2,(Yend_sub_YStart),0x0000);
    //绘制箭头
    bresenham(WAVE_X_LEN/2 - 5,5,WAVE_X_LEN/2,0,0x0000);
    bresenham(WAVE_X_LEN/2 + 5,5,WAVE_X_LEN/2,0,0x0000);
    bresenham(WAVE_X_LEN - 5,(Yend_sub_YStart)/2 - 5,WAVE_X_LEN,(Yend_sub_YStart)/2,0x0000);
    bresenham(WAVE_X_LEN - 5,(Yend_sub_YStart)/2 + 5,WAVE_X_LEN,(Yend_sub_YStart)/2,0x0000);

    _lcd->draw_rectangle(0 + WAVE_X_OFFSET,YStart,WAVE_X_LEN + WAVE_X_OFFSET,Yend);
}


/** 画线函数 **/
void Aurora_Wave::bresenham(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1,uint16_t color){
    int dx = abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
    int dy = abs(y1 - y0), sy = y0 < y1 ? 1 : -1;
    int erro = (dx > dy ? dx : -dy) / 2;
    while(lcd_buff[y0 * WAVE_X_LEN + x0] = color, x0 != x1 || y0 != y1){
        int e2 = erro;
        if(e2 > -dx) { erro -= dy; x0 += sx;}
        if(e2 <  dy) { erro += dx; y0 += sy;}
    }
}

/** 显示马鞍波形 **/
void Aurora_Wave::wave_TaTbTc(uint8_t Mx){
    _lcd->fill(0xffff);
    title_label.show();

    if( Mx == 0 ) {
        motor_label.set_color(0x4abd);
        motor_label.set_pos(10,10);
        motor_label.set_text("左电机");
        ui2foc_msg.wave_type = LFOC_TaTbTc;
    }
    else {
        motor_label.set_color(0xf011);
        motor_label.set_pos(240,10);
        motor_label.set_text("右电机");
        ui2foc_msg.wave_type = RFOC_TaTbTc;
    }

    motor_label.show();

    wave1_label.set_text("Ta");
    wave1_label.show();

    wave2_label.set_hide(DISABLE);
    wave2_label.set_text("Tb");
    wave2_label.show();

    wave3_label.set_text("Tc");
    wave3_label.show();


    ui2foc_msg.type = enable_wave;
    ui2foc_msg.cmd = IS_CMD;

}

/** 显示三相电流**/
void Aurora_Wave::wave_IaIbIc(uint8_t Mx){
    _lcd->fill(0xffff);
    title_label.show();

    if( Mx == 0 ) {
        motor_label.set_pos(10,10);
        motor_label.set_text("左电机");
        ui2foc_msg.wave_type = LFOC_IaIbIc;
    }
    else{
        motor_label.set_pos(240,10);
        motor_label.set_text("右电机");
        ui2foc_msg.wave_type = RFOC_IaIbIc;
    }

    motor_label.show();

    wave1_label.set_text("Ia");
    wave1_label.show();

    wave2_label.set_hide(DISABLE);
    wave2_label.set_text("Ib");
    wave2_label.show();

    wave3_label.set_text("Ic");
    wave3_label.show();


    ui2foc_msg.type = enable_wave;
    ui2foc_msg.cmd = IS_CMD;
}

/** 显示 Iq 和 Id 电流 **/
void Aurora_Wave::wave_IqId(uint8_t Mx){
    _lcd->fill(0xffff);
    title_label.show();

    if( Mx == 0 ) {
        motor_label.set_pos(10,10);
        motor_label.set_text("左电机");
        ui2foc_msg.wave_type = LFOC_IqId;
    }
    else {
        motor_label.set_pos(240,10);
        motor_label.set_text("右电机");
        ui2foc_msg.wave_type = RFOC_IqId;
    }

    motor_label.show();

    wave1_label.set_text("Iq");
    wave1_label.show();

    wave2_label.set_hide(ENABLE);
    wave2_label.show();

    wave3_label.set_text("Id");
    wave3_label.show();



    ui2foc_msg.type = enable_wave;
    ui2foc_msg.cmd = IS_CMD;
}
/** 显示 Ialpha 和 Ibeta 电流 **/
void Aurora_Wave::wave_Ialpha_Ibeta(uint8_t Mx){
    _lcd->fill(0xffff);
    title_label.show();

    if( Mx == 0 ) {
        motor_label.set_pos(10,10);
        motor_label.set_text("左电机");
        ui2foc_msg.wave_type = LFOC_IalphaIbeta;
    }
    else {
        motor_label.set_pos(240,10);
        motor_label.set_text("右电机");
        ui2foc_msg.wave_type = RFOC_IalphaIbeta;
    }

    motor_label.show();

    wave1_label.set_text("Ialpha");
    wave1_label.show();

    wave2_label.set_hide(ENABLE);
    wave2_label.show();

    wave3_label.set_hide(DISABLE);
    wave3_label.set_text("Ibeta");
    wave3_label.show();

    ui2foc_msg.type = enable_wave;
    ui2foc_msg.cmd = IS_CMD;
}
/** 显示 位置 **/
void Aurora_Wave::wave_Position(uint8_t Mx){
    _lcd->fill(0xffff);
    title_label.show();

    if( Mx == 0 ) {
        motor_label.set_pos(10,10);
        motor_label.set_text("左电机");
        ui2foc_msg.wave_type = LFOC_Position;
    }
    else {
        motor_label.set_pos(240,10);
        motor_label.set_text("右电机");
        ui2foc_msg.wave_type = RFOC_Position;
    }

    motor_label.show();

    wave1_label.set_text("Position");
    wave1_label.show();

    wave2_label.set_hide(ENABLE);
    wave2_label.show();

    wave3_label.set_hide(ENABLE);
    wave3_label.show();

    ui2foc_msg.type = enable_wave;
    ui2foc_msg.cmd = IS_CMD;
}

/** 显示 上一个波形  **/
void Aurora_Wave::switch_prev_wave(){
    wave_index = (wave_index == 1) ? wave_index : wave_index-1;

    if(wave_index == 1)
        wave_TaTbTc(0);
    else if(wave_index == 2)
        wave_TaTbTc(1);
    else if( wave_index == 3)
        wave_IaIbIc(0);
    else if(wave_index == 4)
        wave_IaIbIc(1);
    else if(wave_index == 5)
        wave_IqId(0);
    else if(wave_index == 6)
        wave_IqId(1);
    else if(wave_index == 7)
        wave_Ialpha_Ibeta(0);
    else if(wave_index == 8)
        wave_Ialpha_Ibeta(1);
    else if(wave_index == 9)
        wave_Position(0);
    else if(wave_index == 10)
        wave_Position(1);
    else;
    rt_thread_mdelay(20);
}
/** 显示 下一个波形  **/
void Aurora_Wave::switch_next_wave(){
    wave_index = (wave_index == WAVE_MAX) ? wave_index : wave_index+1;
    if(wave_index == 1)
        wave_TaTbTc(0);
    else if(wave_index == 2)
        wave_TaTbTc(1);
    else if( wave_index == 3)
        wave_IaIbIc(0);
    else if(wave_index == 4)
        wave_IaIbIc(1);
    else if(wave_index == 5)
        wave_IqId(0);
    else if(wave_index == 6)
        wave_IqId(1);
    else if(wave_index == 7)
        wave_Ialpha_Ibeta(0);
    else if(wave_index == 8)
        wave_Ialpha_Ibeta(1);
    else if(wave_index == 9)
        wave_Position(0);
    else if(wave_index == 10)
        wave_Position(1);
    else;
    rt_thread_mdelay(20);
}